import os

from qiniu import Auth, put_file, etag
from django.conf import settings
import uuid
from libs.send_qinuiyun.settings import ACCESS_KEY,SECRET_KEY,BUCKED_NAME,DOMAIN_NAME
from youzhu_api.settings import dev


def qiniu_token(bucked_name,file_name):    #bucked_name空间名

    q = Auth(access_key=ACCESS_KEY,
             secret_key=SECRET_KEY)

    token = q.upload_token(bucked_name,file_name,3600)

    return token


def upload_img(file_path):
    """
    收集本地信息到云服务器上
    参考地址：https://developer.qiniu.com/kodo/sdk/1242/python
    """
    # 指定图片名称
    file_name = '{}.png'.format(uuid.uuid4())
    # 指定上传空间，获取token   #生成上传 Token，可以指定过期时间等
    token = qiniu_token(BUCKED_NAME,file_name)

    ret, info = put_file(token,  file_name, file_path)    #file_path本地要上传的图片路径
    img_url = DOMAIN_NAME + ret.get('key')
    return img_url


# if __name__ == '__main__':
#
#     file_path = os.path.dirname(settings.BASE_DIR)+"/media/photo/R-C.png"

    # print(upload_img(file_path))
